--local wifiApi = require "wifiApi"
--local timeApi = require "timeApi"
--local udpApi = require "udpApi"
---@class terminal
local terminal = {
  wifi = wifiApi,
  time = timeApi,
  udp = udpApi
}

function terminal:init()

end

--- @param s userdata
--- @param port string
--- @param ip string
--- @param args table<number, string>
function terminal:shell(s, port, ip, args)
  if #args<2 then return end

  local api = self[args[1]]
  if not api then
    s:send(port, ip, "no api: " .. args[1])
    return
  end

  local fun = api[args[2]]
  if not fun then
    s:send(port, ip, "no func: " .. args[2])
    return
  end

  if #args<3 then
    s:send(port, ip, args[1] .. "=>".. args[2] .. ": ".. (fun(api) or "ok"))
    return
  end
  for i = 3, #args, 2 do
    -- key
    if i + 1 > #args then return end
    api[args[i]] = args[i+1]
  end
  s:send(port, ip, args[1] .. "=>".. args[2] .. ": ".. (fun(api) or "ok"))

end
function terminal:udpReceiveListener(s, data, port, ip)
  self:shell(s, port, ip, Split(data, " "))
--  s:send(port,ip,"this is terminal: ".. #(Split(data, " ")))
end
return terminal
